#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <dirent.h>
#include <unistd.h>
#include <vector>
#include <sstream>
#include <fstream>
#include <sys/io.h>
#include <sys/times.h>
#include <iomanip> // setw()
using namespace std;

#include "Core"
using namespace Eigen;

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/eigen.hpp>
using namespace cv;

#include "opencv2/ximgproc.hpp"
using namespace cv::ximgproc;

#include "segFlag.hpp"
using namespace segfg;

void rotateTest();

/******************************
*/
int main(int argc, char** argv)
{
	cout << ">> ----" << "\n" << endl;
	cv::Mat src = imread("./data/d_7.jpg", IMREAD_GRAYSCALE);
//	segment_flag(src);
	rotateTest();
	cout << "\n" << ">> ----" << endl;
	return 0;
}

/******************************
*/
void rotateTest() {
	cv::Mat src(cv::Size(240, 160), CV_8UC1, Scalar::all(0));
	int edge = 50;
	cv::Rect rct(edge, edge, src.cols - edge * 2, src.rows - edge* 2);
	cv::rectangle(src, rct, cv::Scalar(255), -1, 8, 0);
	cv::rectangle(src, Rect(rct.x + 5, rct.y + 5, rct.width - 10, 4), 
	cv::Scalar(0, 0, 0), -1, 8, 0);
	for (int i = 0; i < 12; i++) {
		cv::Mat rotateImg;
		if (1) {
			Point2f center(src.cols / 2, src.rows / 2);
			float angle = 30.f * i;
			cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.f);
			cv::RotatedRect box(center, cv::Size(src.rows, src.cols), angle);
			cv::Rect bbox = box.boundingRect();
			rot.at<double>(0, 2) += bbox.width / 2.f - center.x;
			rot.at<double>(1, 2) += bbox.height/ 2.f - center.y;
			cv::warpAffine(src, rotateImg, rot, cv::Size(bbox.width, bbox.height));
		}
		
		vector<vector<Point> > contours;
		vector<Vec4i> hierarchy;
		findContours(rotateImg, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
		cv::RotatedRect box = minAreaRect(contours[0]);
		cout << setw(5) << (int)box.angle << "    "
		     << setw(5) << (int)box.size.width << "    "
		     << setw(5) << (int)box.size.height << endl;
		
		
/*		imshow("win", rotateImg);
		waitKey(0);*/
	}
	
}


